package com.ys.controller.api.storage;

import com.ys.config.MenuId;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.storage.TccWarehouse;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.storage.TccWarehouseService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理
 * fxz
 */
@Controller
@RequestMapping("/api/inter/tccWarehouse")
public class ApiInterTccWarehouseController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccWarehouseController.class);

    @Autowired
    private CommonApiService<TccWarehouse> commonApiService;
    
    Long menuId = MenuId.TCC_Warehouse;
    
    @Autowired
    private TccWarehouseService tccWarehouseService;

    /**
     * 设置查询的cond的sql设置
     */
    private TccWarehouse setSeachSql(TccWarehouse cond){
        cond.addLeftJoin("TB_Country tb ON tb.Code = A.CountryCode");
        cond.addField("tb.name countryName");
        cond.addField("tb.code countryCode");
        cond.setOrderby("A.Id DESC");
        return  cond;
    }

    @RequiresPermissions("tccWarehouse:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA searchDetail(@ModelAttribute TccWarehouse cond){

        cond = setSeachSql(cond);

        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }


    /*
     * 导出
     */
    @RequiresPermissions("tccWarehouse:export")
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccWarehouse cond) throws Exception{
        cond.setOrderby("A.Id DESC");

        cond.setPage(0).setSize(999999);

        List<Map<String, Object>> listData = cond.queryMap();

        List<YsCol> listField = new YsCol()
                .setMenuId(menuId)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();


        ExportExcelUtils<TccWarehouse> util = new ExportExcelUtils<TccWarehouse>(new TccWarehouse());

        String toFilePath = util.exportExcel(listData, listField, "仓库管理");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }
    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("考勤管理新增传参：" + params);
            //修改
            return tccWarehouseService.add(params);
        } catch (Exception e) {
            logger.error("考勤管理新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 编辑
     */

    @PostMapping("/toEdit")
    @ResponseBody
    public R edit(@RequestParam Map<String, Object> params){
        try {
            return tccWarehouseService.edit(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 仓库管理弹出框
     */
    @RequestMapping("/searchForModal1")
    @ResponseBody
    public LayuiTableVoJA searchForModal1(@ModelAttribute TccWarehouse cond){

        commonApiService.dataAuth(MenuId.TCC_Warehouse, cond);

        cond.addLeftJoin("TB_Country tb ON tb.code = A.countryCode")
            .addField("tb.name countryName")
            .setStatus(1);
        return commonApiService.searchLay(cond);
    }
}
